package puziran.sofa.http.client;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;

public class HttpLoadBalanceNew
{
  private Hashtable allClienct = new Hashtable();
  private Collection allHTTPURLs;
  private Collection allErrURLs;
  private Collection allLiveURLs;
  private String[] warArray;
  private int refreshSeconds = 60;
  private static long latestCheckTime = 0L;
  private String page600 = "600.html";
  private String page601 = "601.html";
  private String page602 = "602.html";
  private String page603 = "603.html";
  private String page604 = "604.html";

  private static HttpLoadBalanceNew dis = new HttpLoadBalanceNew();

  private HttpLoadBalanceNew()
  {
    this.warArray = HttpPropertyUtilNew.getValue("war").split(",");

    this.allErrURLs = new ArrayList();

    int esize = Integer.parseInt(HttpPropertyUtilNew.getValue("err.size"));

    for (int i = 0; i < esize; i++) {
      String s = HttpPropertyUtilNew.getValue("err" + i);
      if ((s != null) && (s.trim().length() != 0)) {
        this.allErrURLs.add(s);
      }
    }
    this.refreshSeconds = Integer.parseInt(HttpPropertyUtilNew.getValue("refresh.seconds"));
  }

  public static HttpLoadBalanceNew getInstance()
  {
    return dis;
  }

  public String route(String url)
  {
    if (System.currentTimeMillis() - latestCheckTime > this.refreshSeconds * 1000) {
      checkURLs(url);
    }

    if (this.allLiveURLs.size() == 0) {
      if (this.allErrURLs.size() == 0) {
        return this.page601;
      }
      int random = (int)Math.random() * this.allErrURLs.size();
      String eurl = (String)this.allErrURLs.toArray()[random];
      return eurl;
    }
    double rd = Math.random();

    int random = (int)Math.floor(rd * this.allLiveURLs.size());
    String lurl = (String)this.allLiveURLs.toArray()[random];
    if (isLive(lurl))
    {
      return lurl;
    }
    checkURLs(url);
    if (this.allLiveURLs.size() == 0) {
      random = (int)Math.random() * this.allErrURLs.size();
      lurl = (String)this.allErrURLs.toArray()[random];
      return lurl;
    }

    random = (int)Math.random() * this.allLiveURLs.size();
    lurl = (String)this.allLiveURLs.toArray()[random];

    return lurl;
  }

  public static void main(String[] args)
  {
    String url = getInstance().route("party/partycs/list");
  }

  private String[] getWarNameNumAarry(String url)
  {
    String[] warNameNumAarry = new String[2];
    if (!url.contains("/")) {
      System.out.println("url=" + url + " 有误，请检查");
      return null;
    }
    String warName = url.substring(0, url.indexOf("/"));

    for (int i = 0; i < this.warArray.length; i++) {
      String warNameNum = this.warArray[i];
      warNameNumAarry = warNameNum.split(":");
      if (warNameNumAarry[0].equals(warName)) {
        return warNameNumAarry;
      }
    }
    return null;
  }

  private void checkURLs(String url)
  {
    this.allHTTPURLs = new ArrayList();
    this.allLiveURLs = new ArrayList();

    String[] warNameNumArray = getWarNameNumAarry(url);
    if (warNameNumArray == null) {
      return;
    }
    String warName = warNameNumArray[0];
    int warNum = Integer.parseInt(warNameNumArray[1]);

    for (int i = 0; i < warNum; i++) {
      String s = HttpPropertyUtilNew.getValue(warName + i);
      if ((s != null) && (s.trim().length() != 0)) {
        this.allHTTPURLs.add(s);
      }
    }

    Iterator it = this.allHTTPURLs.iterator();
    while (it.hasNext()) {
      String urls = (String)it.next();
      if (isLive(urls))
        this.allLiveURLs.add(urls);
    }
  }

  private boolean isLive(String url)
  {
    for (int i = 0; i < 3; ) {
      try {
        HttpURLConnection urlconn = (HttpURLConnection)new URL(url).openConnection();
        int code = urlconn.getResponseCode();
        if (code == 200)
          return true;
      }
      catch (Exception localException)
      {
        i++;
      }

    }

    return false;
  }
}